package vn.vfriends.games.jpa.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.*;
import javax.validation.constraints.Size;
import org.eclipse.persistence.annotations.ConversionValue;
import org.eclipse.persistence.annotations.Convert;
import org.eclipse.persistence.annotations.ObjectTypeConverter;

/**
 *
 * @author tuan@vfriends.vn
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "Role.findByName", query = "SELECT r FROM Role r WHERE r.name = :name")})
public class Role implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false)
    private Long id;
    
    @Size(min = 1, max = 100)
    @Column(name = "NAME", unique=true, length=100)
    private String name;
    
    @Size(max=200)
    @Column(length=200)
    private String description;
    
    @OneToMany(mappedBy="role", fetch = FetchType.LAZY)
    private List<Page> pages = new ArrayList<Page>();
    
    @ElementCollection
    @Column(name="EMAIL")
    private Set<String> userEmails = new HashSet<String> ();
    
    public enum RoleCode {
        ADMINISTRATOR("ADMIN"), EDITOR("EDITOR"),CUSTOMER("CUS");
        
        private String code;
        
        private RoleCode(String code) {
            this.code = code;
        }

        public String getCode() {
            return code;
        }
        
        @Override
        public String toString() {
            return code;
        }
    }
    
    @ObjectTypeConverter(name = "roleCodeConverter", objectType = RoleCode.class, dataType = String.class, conversionValues = {
        @ConversionValue(objectValue = "ADMINISTRATOR", dataValue = "ADMIN"),
        @ConversionValue(objectValue = "EDITOR", dataValue = "EDITOR"),
        @ConversionValue(objectValue = "CUSTOMER", dataValue = "CUS")})
    
    @Convert("roleCodeConverter")
    @Column(unique=true, length=20)
    private RoleCode code;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<String> getUserEmails() {
        return userEmails;
    }

    public void setUserEmails(Set<String> userEmails) {
        this.userEmails = userEmails;
    }

    public List<Page> getPages() {
        return pages;
    }

    public void setPages(List<Page> pages) {
        this.pages = pages;
    }

    public RoleCode getCode() {
        return code;
    }

    public void setCode(RoleCode code) {
        this.code = code;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Role other = (Role) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 79 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }
    
}
